package dcds2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Mesan
 */
public class EditTransactionWindow extends javax.swing.JFrame {
    DefaultComboBoxModel monthCombo, dayCombo;
    String[] listPatients, procedures;
    Double[] procedurePrices;
    int procedureCount;
    int transacID;
    int rowsAdded = 0;
    DefaultTableModel modelProceduresList = new DefaultTableModel(new Object[][]{}, new String[] {"Procedure","Dentist","Price"});
    public String[] months = {"January","February", "March","April", "May", "June", "July", "August", "September"
                        , "October", "November", "December"};
     public String[] days31 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30", "31"};
    
     public String[] days30 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30"};
     public String[] days28 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28"};
     public String[] days29 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29"};
    
    /** Creates new form EditTransactionWindow */
    public EditTransactionWindow(int transID) {
        String[] MonthString = new String[]{"January","February","March","April","May","June","July","August","September","October","November","December"};
        String[] DayString = new String[] {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
        monthCombo = new DefaultComboBoxModel(MonthString);
        dayCombo =new DefaultComboBoxModel(DayString);
        transacID = transID;
        initComponents();
        setLocationRelativeTo(null);
        
        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM transactions WHERE transactionID = '"+transID+"'");
            ResultSet rs = pstmt.executeQuery();
            
            rs.next();
            
            String SuperDate = rs.getString("transactionDate");
            StringTokenizer str = new StringTokenizer(SuperDate, ", ");
            String tMonth = str.nextToken();
            String tDay = str.nextToken();
            String tYear = str.nextToken();
            String patientName = rs.getString("pName");
            int i;
            for(i = 0; i < MonthString.length && !MonthString[i].equals(tMonth); i++) {
            }
            comboMonth.setSelectedIndex(i);
            for(i = 0; i < DayString.length && !DayString[i].equals(tDay); i++) {
            }
            comboDay.setSelectedIndex(i);
            txtYear.setText(tYear);
            
            pstmt = conn.prepareStatement("SELECT * FROM patient ORDER BY pLastName ASC");
            rs = pstmt.executeQuery();
            rs.last();
            listPatients = new String[rs.getRow()];
            rs.beforeFirst();
            i = 0;
            while (rs.next()) {

                String fName = rs.getString("pFirstName");
                String mName = rs.getString("pMiddleName");
                String lName = rs.getString("pLastName");
                String pName = lName + ", " + fName + ", " + mName;
                listPatients[i] = pName;
                System.out.println(listPatients[i]);

                i++;
            }
            comboName.setModel(new DefaultComboBoxModel(listPatients));
            
            for(i = 0; i < listPatients.length && !listPatients[i].equals(patientName); i++){
            }
            comboName.setSelectedIndex(i);
            
            i = 0;

            pstmt = conn.prepareStatement("SELECT * FROM dcds.procedures");

            rs = pstmt.executeQuery();
            rs.last();
            procedures = new String[rs.getRow()];
            procedurePrices = new Double[rs.getRow()];
            rs.beforeFirst();

            while (rs.next()) {
                procedures[i] = rs.getString("procedureName");
                procedurePrices[i] = rs.getDouble("procedurePrice");
                i++;
            }
            
            comboProcedure.setModel(new DefaultComboBoxModel(procedures));
            txtPrice.setText(procedurePrices[comboProcedure.getSelectedIndex()].toString());
            
            i = 0;
            
            pstmt = conn.prepareStatement("SELECT * FROM transprocdetails WHERE transactionID = '"+transID+"'");

            rs = pstmt.executeQuery();
            rs.last();
            procedureCount = rs.getRow();
            rs.beforeFirst();

            while(rs.next()){

                String procedureName = rs.getString("procedureName");
                String dentistName = rs.getString("dentistName");
                Double procedurePrice = rs.getDouble("procedurePrice");

                modelProceduresList.addRow(new Object[] {"","",""});
                tblProceduresList.setValueAt(procedureName, i, 0);
                tblProceduresList.setValueAt(dentistName, i, 1);
                tblProceduresList.setValueAt(procedurePrice, i, 2);

                i++;
            }
            System.out.println(modelProceduresList.getRowCount());
            
        }
        catch (Exception e) {
            
        }
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        comboMonth = new javax.swing.JComboBox();
        comboDay = new javax.swing.JComboBox();
        txtYear = new javax.swing.JTextField();
        btnAddProcedure = new javax.swing.JButton();
        comboName = new javax.swing.JComboBox();
        comboProcedure = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProceduresList = new javax.swing.JTable();
        btnCancel = new javax.swing.JButton();
        btnConfirm = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        comboDentist = new javax.swing.JComboBox();
        btnDeleteProcedure = new javax.swing.JButton();
        toggleEdit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - View/Edit Transaction");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setFont(new java.awt.Font("Century", 0, 11));
        jLabel1.setText("Date:");

        comboMonth.setFont(new java.awt.Font("Century", 0, 11));
        comboMonth.setModel(monthCombo);
        comboMonth.setEnabled(false);
        comboMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboMonthActionPerformed(evt);
            }
        });

        comboDay.setFont(new java.awt.Font("Century", 0, 11));
        comboDay.setModel(dayCombo);
        comboDay.setEnabled(false);

        txtYear.setFont(new java.awt.Font("Century", 0, 11));
        txtYear.setEnabled(false);
        txtYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtYearActionPerformed(evt);
            }
        });

        btnAddProcedure.setFont(new java.awt.Font("Century", 0, 11));
        btnAddProcedure.setText("Add Procedure");
        btnAddProcedure.setEnabled(false);
        btnAddProcedure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddProcedureActionPerformed(evt);
            }
        });

        comboName.setFont(new java.awt.Font("Century", 0, 11));
        comboName.setEnabled(false);

        comboProcedure.setFont(new java.awt.Font("Century", 0, 11));
        comboProcedure.setEnabled(false);
        comboProcedure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboProcedureActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Century", 0, 11));
        jLabel2.setText("Name:");

        jLabel3.setFont(new java.awt.Font("Century", 0, 11));
        jLabel3.setText("Procedure:");

        jLabel4.setFont(new java.awt.Font("Century", 0, 11));
        jLabel4.setText("Dentist:");

        txtPrice.setFont(new java.awt.Font("Century", 0, 11)); // NOI18N
        txtPrice.setEnabled(false);

        tblProceduresList.setFont(new java.awt.Font("Century", 0, 11));
        tblProceduresList.setModel(modelProceduresList);
        tblProceduresList.setEnabled(false);
        jScrollPane1.setViewportView(tblProceduresList);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonBack.png"))); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnConfirm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonConfirm.png"))); // NOI18N
        btnConfirm.setEnabled(false);
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Century", 0, 11));
        jLabel5.setText("Price:");

        comboDentist.setFont(new java.awt.Font("Century", 0, 11));
        comboDentist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dr. Rex T. Mayor", "Dr. Rowena B. Mayor" }));
        comboDentist.setEnabled(false);

        btnDeleteProcedure.setFont(new java.awt.Font("Century", 0, 11));
        btnDeleteProcedure.setText("Delete Procedure");
        btnDeleteProcedure.setEnabled(false);
        btnDeleteProcedure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteProcedureActionPerformed(evt);
            }
        });

        toggleEdit.setFont(new java.awt.Font("Century", 0, 11));
        toggleEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonEdit.png"))); // NOI18N
        toggleEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(comboName, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(comboDentist, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnAddProcedure)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                    .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(comboMonth, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                    .addComponent(comboProcedure, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGap(18, 18, 18)
                                                    .addComponent(jLabel5)))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(toggleEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(50, 50, 50))
                            .addComponent(btnDeleteProcedure))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboDentist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboProcedure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddProcedure)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeleteProcedure)
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnConfirm)
                        .addComponent(btnCancel))
                    .addComponent(toggleEdit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtYearActionPerformed
        int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
        }else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
}//GEN-LAST:event_txtYearActionPerformed

    private void btnAddProcedureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddProcedureActionPerformed
        try {
            Double procedurePrice = Double.parseDouble(txtPrice.getText());
            /*try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 0;
            */
            String procedureName = comboProcedure.getSelectedItem().toString();
            String dentistName = comboDentist.getSelectedItem().toString();
            
            procedureCount++;
            //rowsAdded++;
            /*
            PreparedStatement pstmt2 = conn.prepareStatement("INSERT INTO transprocdetails (transactionID, procedureNo, procedureName, dentistName, procedurePrice) VALUES ('"+transacID+"','"+procedureCount+"','"+procedureName+"','"+dentistName+"','"+procedurePrice+"')");
            
            pstmt2.executeUpdate();
            System.out.println("Procedure Count: "+procedureCount);
            conn.close();*/

            modelProceduresList.addRow(new Object[] {"","","",});
            if(modelProceduresList.getRowCount() == 0) {
                tblProceduresList.setValueAt(procedureName, modelProceduresList.getRowCount(), 0);
                tblProceduresList.setValueAt(dentistName, modelProceduresList.getRowCount(), 1);
                tblProceduresList.setValueAt(procedurePrice, modelProceduresList.getRowCount(), 2);
            }else{
                tblProceduresList.setValueAt(procedureName, modelProceduresList.getRowCount()-1, 0);
                tblProceduresList.setValueAt(dentistName, modelProceduresList.getRowCount()-1, 1);
                tblProceduresList.setValueAt(procedurePrice, modelProceduresList.getRowCount()-1, 2);
            }
            
            /*//tableRows++;
            } catch (Exception e) {
            e.printStackTrace();
            }*/ 
        }catch (NumberFormatException nfe){
            JOptionPane.showMessageDialog(new JFrame(), "Please enter a valid price!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
        }
}//GEN-LAST:event_btnAddProcedureActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        /*for(int i = rowsAdded; i > 0; i--) {
            try {
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                PreparedStatement pstmt = conn.prepareStatement("DELETE FROM transprocdetails WHERE procedureNo = '"+procedureCount+"' AND transactionID = '"+transacID+"'");
                procedureCount--;
                pstmt.executeUpdate();
                conn.close();

            }catch (Exception e) {
                e.printStackTrace();
            }
        }*/    
        dispose();
        new MainWindow(3).setVisible(true);
}//GEN-LAST:event_btnCancelActionPerformed

    private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed
        JFrame frame = new JFrame();
        int selection = JOptionPane.showConfirmDialog(frame, "Are you sure?", "Confirmation", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
        
        if(selection==JOptionPane.YES_OPTION){
            
            try{
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                PreparedStatement pstmt = conn.prepareStatement("DELETE FROM transprocdetails WHERE transactionID = '"+transacID+"'");
                pstmt.executeUpdate();
                int i = 1;
                while(i <= tblProceduresList.getRowCount()){
                    
                    pstmt = conn.prepareStatement("INSERT INTO transprocdetails (transactionID, procedureNo, procedureName, dentistName, procedurePrice) VALUES ('"+transacID+"','"+i+"','"+tblProceduresList.getValueAt(i-1, 0).toString()+"','"+tblProceduresList.getValueAt(i-1, 1).toString()+"','"+Double.parseDouble(tblProceduresList.getValueAt(i-1, 2).toString())+"')");
                    pstmt.executeUpdate();
                    i++;
                }
                
                String pName = comboName.getSelectedItem().toString();
                StringTokenizer str = new StringTokenizer(pName, ",");

                String lName = str.nextToken();
                String fName = str.nextToken().trim();
                String mName = str.nextToken().trim();

                pstmt = conn.prepareStatement("SELECT patientID, pLastName, pFirstName, pMiddleName FROM patient WHERE (pLastName = '"+lName+"' AND pFirstName = '"+fName+"' AND pMiddleName = '"+mName+"')");
                ResultSet rs = pstmt.executeQuery();
                rs.next();
                int patientID = rs.getInt("patientID");
                
                pstmt = conn.prepareStatement("UPDATE transactions SET patientID = '"+patientID+"', pName = '"+comboName.getSelectedItem().toString()+"', transactionDate = '"+comboMonth.getSelectedItem().toString()+" "+comboDay.getSelectedItem().toString()+ ", "+txtYear.getText()+"' WHERE transactionID = '"+transacID+"'");
                pstmt.executeUpdate();
                conn.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            JOptionPane.showMessageDialog(frame, "Changes applied!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
        }
        dispose();
        new MainWindow(3).setVisible(true);
}//GEN-LAST:event_btnConfirmActionPerformed

    private void btnDeleteProcedureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteProcedureActionPerformed
        if(modelProceduresList.getRowCount() != 0) {
            
                procedureCount--;
                tblProceduresList.setValueAt("", modelProceduresList.getRowCount()-1, 0);
                tblProceduresList.setValueAt("", modelProceduresList.getRowCount()-1, 1);
                tblProceduresList.setValueAt("", modelProceduresList.getRowCount()-1, 2);
                modelProceduresList.removeRow(modelProceduresList.getRowCount()-1);
        }
        else
            JOptionPane.showMessageDialog(new JFrame(), "There are no more procedures to delete!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
}//GEN-LAST:event_btnDeleteProcedureActionPerformed

    private void comboMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboMonthActionPerformed
          int index = comboMonth.getSelectedIndex()+1;
    if(index == 2){
        int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
}       else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
        
    } else if(index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 ||index == 12){
        comboDay.setModel(new DefaultComboBoxModel(days31));
    } else 
        comboDay.setModel(new DefaultComboBoxModel(days30));
    }//GEN-LAST:event_comboMonthActionPerformed

    private void toggleEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleEditActionPerformed
        comboMonth.setEnabled(true);
        comboDay.setEnabled(true);
        txtYear.setEnabled(true);
        comboName.setEnabled(true);
        comboDentist.setEnabled(true);
        comboProcedure.setEnabled(true);
        txtPrice.setEnabled(true);
        btnAddProcedure.setEnabled(true);
        btnDeleteProcedure.setEnabled(true);
        tblProceduresList.setEnabled(true);
        btnConfirm.setEnabled(true);
    }//GEN-LAST:event_toggleEditActionPerformed

    private void comboProcedureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboProcedureActionPerformed
        txtPrice.setText(procedurePrices[comboProcedure.getSelectedIndex()].toString());
    }//GEN-LAST:event_comboProcedureActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditTransactionWindow(1).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddProcedure;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JButton btnDeleteProcedure;
    private javax.swing.JComboBox comboDay;
    private javax.swing.JComboBox comboDentist;
    private javax.swing.JComboBox comboMonth;
    private javax.swing.JComboBox comboName;
    private javax.swing.JComboBox comboProcedure;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tblProceduresList;
    private javax.swing.JButton toggleEdit;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtYear;
    // End of variables declaration//GEN-END:variables

}
